import user from '../../../utils/user.js'
import {
    base64ToWxfile
} from '../../../utils/util.js'
import {
    createInviteQrCode,
    getUserAvatar
} from '../../../api/app.js'
const app = getApp()
Page({
    data: {
        codem: '',
        avatarUrl: '',
        name: '',
        supplierName: '',
        qrcode: '',
        projectName: '',
        projectId: '',
        bacpath: ''
    },
    onReady() {
        user.waitLogin().then(() => {
            this.setData({
                name: user.userName,
                supplierName: user.companyName
            })

        })

    },
    onLoad(option) {
        console.log(option)
        wx.getImageInfo({
            src: 'https://bullet001.oss-cn-shanghai.aliyuncs.com/file/payManager466b6a6a-bff6-4108-a29e-1729fd0e3c87.png',
            // url: 'https://bullet001.oss-cn-shanghai.aliyuncs.com/file/payManager466b6a6a-bff6-4108-a29e-1729fd0e3c87.png',
            success: (res) => {
                console.log(res, 'uhdhiudui');
                this.setData({
                    bacpath: res.path
                })
            }
        })
        this.setData({
            projectName: option.projectName,
            projectId: option.projectId
        })
        this.createInviteQrCode(option)
    },
    createInviteQrCode(option) {
        createInviteQrCode({
            appId: app.globalData.appId,
            page: 'pages/index/index',
            scene: 'uId=' + user.userId + '&cId=' + user.companyId + (option.projectId ? '&pId=' + option.projectId : ''),
            width: 280
        }).then(res => {
            console.log(res)
            this.setData({
                codem: res
            })
            var qrCode = res // 后台返回的base64图片流
            base64ToWxfile(qrCode, Math.ceil(Math.random() * 10000000000) + '.png').then(path => {
                console.log('path', path)
                this.setData({
                    qrcode: path // 绘制图片使用的图片路径
                })
                this.saveQrCode()
            })

        })
    },
    saveQrCode() {
        console.log('saveQrcode')
        // 网络文件转base64
        getUserAvatar({
            userId: user.userId
        }).then(avatarUrl => {
            const base64 = avatarUrl
            // base64转本地文件
            base64ToWxfile(base64, 'avatar.png').then(res => {
                // 处理头像为空
                const avatarUrl = (base64 === 'undefind' || !base64) ?
                    '/assets/img/avatar.png' : res
                this.setData({
                    avatarUrl: avatarUrl
                })
                const ctx = wx.createCanvasContext('myCanvas')
                ctx.rect(0, 0, 375, 560)
                ctx.setFillStyle('#fff')
                ctx.fill()
                ctx.clip() // 剪切
                ctx.drawImage(this.data.bacpath, 0, 0, 375, 560, 0, 0)
                // 字体
                ctx.font =
                    'normal bold 24px Monospaced Number,Chinese Quote,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,PingFang SC,Hiragino Sans GB,Microsoft YaHei,Helvetica Neue,Helvetica,Arial,sans-serif'
                // 居中	
                ctx.textAlign = 'center'
                // 姓名
                ctx.setFillStyle('#202536')
                ctx.fillText("我是" + user.userName, 187, 250)
                ctx.font =
                    'normal bold 17px Monospaced Number,Chinese Quote,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,PingFang SC,Hiragino Sans GB,Microsoft YaHei,Helvetica Neue,Helvetica,Arial,sans-serif'
                ctx.fillText('「' + (this.data.projectName || user.companyName) + '」', 187, 100)
                // 二维码放置
                ctx.drawImage(this.data.qrcode, 110, 276, 158, 158, 0, 0)
                // 头像
                ctx.beginPath()
                ctx.arc(190, 170, 50, 0, Math.PI * 2, false)
                ctx.closePath()
                ctx.setFillStyle('#fff')
                ctx.fill()
                ctx.clip()
                ctx.drawImage(avatarUrl, 140, 120, 100, 100, 0, 0)
                ctx.draw()
                ctx.beginPath()
                ctx.draw(true)
            })
        })
    },
    savecanvas() {
        wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: 375,
            height: 560,
            canvasId: 'myCanvas',
            success: function (res) {
                console.log(res)
                let img = res.tempFilePath
                wx.saveImageToPhotosAlbum({
                    filePath: img,
                    success() {
                        app.toast('成功保存', 'success')
                    },
                    fail(err) {
                        app.toast('保存失败', err)
                    }
                })
                console.log(res.tempFilePath)
            },
            fail(err) {
                console.log(err)
            }
        })
    },
    onShareAppMessage() {
        console.log(this.data.projectName, '123456')
        return {
            title: user.userName + '邀请您加入「' + (this.data.projectName || user.companyName) + '」' + (this.data.projectName ? '名单管理' : '项目'),
            path: '/pages/index/index?scene=' + encodeURIComponent('uId=' + user.userId + '&cId=' + user.companyId + (this.data.projectId ?
                ('&pId=' + this.data.projectId) : '')),
            imageUrl: '/packagePay/assets/img/resident/share.png'
        }
    }
})